package top.shaoxia.pwdm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.shaoxia.pwdm.pojo.PwdmDetailsDO;
import top.shaoxia.pwdm.repositories.PwdmDetailsRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wjc
 * @date 2022/2/19
 */
@Service
@Transactional
public class PwdmDetailsService {
    @Autowired
    private PwdmDetailsRepository pwdmDetailsRepository;

    public List<PwdmDetailsDO> listAllDetails(PwdmDetailsDO detailsDO) {
        List<PwdmDetailsDO> list;
        if (detailsDO.getFolderId() == null) {
            if (detailsDO.getStar() != null) {
                list = pwdmDetailsRepository.findByStarOrderBySnDesc(detailsDO.getStar());
            } else if (detailsDO.getDeleted() != null) {
                list = pwdmDetailsRepository.findByDeletedOrderBySnDescFolderIdDesc(detailsDO.getDeleted());
            } else {
                Iterable<PwdmDetailsDO> all = pwdmDetailsRepository.findAll();
                list = new ArrayList<>();
                all.forEach(list::add);
            }
        } else {
            list = pwdmDetailsRepository.findByFolderIdAndDeletedOrderBySnDesc(detailsDO.getFolderId(), detailsDO.getDeleted());
        }
        return list;
    }

    public PwdmDetailsDO save(PwdmDetailsDO detailsDO) {
        return pwdmDetailsRepository.save(detailsDO);
    }

    public void delete(PwdmDetailsDO detailsDO) {
        pwdmDetailsRepository.delete(detailsDO);
    }
}
